#include "widget.h"
#include "ui_widget.h"

enum DataType {
    IntArray,
    String
};


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    newidget = new QWidget ();

    newidget->setWindowTitle("服务器");

    plyr_num=1;

    m_s = new QTcpServer(this);    

    btn_start = new QPushButton("创建对局");
    btn_tcps = new QPushButton("创建服务器");

    connect(this, &Widget::chips, this, &Widget::if_mechip);

    connect(btn_tcps, SIGNAL(clicked()), this, SLOT(start_listen()));
    connect(m_s, &QTcpServer::newConnection, this, [=](){
        QTcpSocket *socket = m_s->nextPendingConnection();
        m_tcp.append(socket);
        w_record->appendPlainText("有新玩家加入游戏......");

        plyr_num++;

        QByteArray data;
        QDataStream out(&data, QIODevice::WriteOnly);
        //4代表整型变量
        out << 4 << plyr_num;

        m_tcp.at(plyr_num-2)->write(data);
    });
    connect(btn_start, SIGNAL(clicked()), this, SLOT(start_game()));

    HLayou_01 = new QHBoxLayout;
    HLayou_02 = new QHBoxLayout;
    HLayou_03 = new QHBoxLayout;
    HLayou_04 = new QHBoxLayout;

    VLayou01 = new QVBoxLayout();

    w_port = new QTextEdit;

    w_record = new QPlainTextEdit;

    w_record->setReadOnly(true);

    w_port->setText("9933");

    HLayou_01->addWidget(w_port);
    HLayou_01->addWidget(btn_tcps);

    HLayou_02->addWidget(w_record);

    HLayou_03->addWidget(btn_start);

    VLayou01->addLayout(HLayou_01);
    VLayou01->addLayout(HLayou_02);
    VLayou01->addLayout(HLayou_03);

    newidget->setLayout(VLayou01);

    btn_start->setEnabled(false);

    newidget->show();

    ui->setupUi(this);

    for(int i =0 ;i<15 ;i++){
        Shuffled[i]=i+1;
    }

    //初始化w_deck
    for(int i=0;i<15;i++){
        w_card[i] = new Card (i+1);
    }
    for(int i=0;i<6;i++){
        w_hand[i] = new Hand (w_card[i]);
    }
    for(int i=0;i<6;i++){
        w_plyr[i] = new Player (w_hand[i]);
    }
    w_deck = new Deck(w_plyr);

    pushbtn = new QPushButton("开始游戏");
    pushbtn_chip = new QPushButton("开始加注");
    btn_show = new QPushButton("开牌");
    btn_change = new QPushButton("换牌");
    btn_check = new QPushButton("确认");
    btn_reject = new QPushButton("取消");
    btn_pub = new QPushButton("公示");
    btn_raise = new QPushButton("加注");
    btn_call = new QPushButton("跟注");
    btn_fold = new QPushButton("弃牌");
    btn_all = new QPushButton("all-in");

    btn_change->setEnabled(false);
    btn_pub->setEnabled(false);
    btn_show->setEnabled(false);
    btn_check->setEnabled(false);
    btn_reject->setEnabled(false);
    btn_raise->setEnabled(false);
    btn_call->setEnabled(false);
    btn_fold->setEnabled(false);
    btn_all->setEnabled(false);

    connect(pushbtn, SIGNAL(clicked()), this, SLOT(game_start()));
    connect(pushbtn_chip, SIGNAL(clicked()), this, SLOT(start_chip()));
    connect(btn_change, SIGNAL(clicked()), this, SLOT(if_change()));
    connect(btn_show, SIGNAL(clicked()), this, SLOT(show_card()));
    connect(btn_check, SIGNAL(clicked()), this, SLOT(c_change()));
    connect(btn_reject, SIGNAL(clicked()), this, SLOT(r_change()));
    connect(btn_pub, SIGNAL(clicked()), this, SLOT(if_pub()));
    connect(btn_raise, SIGNAL(clicked()), this, SLOT(w_raise()));
    connect(btn_call, SIGNAL(clicked()), this, SLOT(w_call()));
    connect(btn_fold, SIGNAL(clicked()), this, SLOT(w_fold()));
    connect(btn_all, SIGNAL(clicked()), this, SLOT(w_all()));

    spin_raise = new QSpinBox();
    spin_raise->setRange(10, 1000);

    record = new QPlainTextEdit;

    txt[0] = "玩家1";
    txt[1] = "玩家2";
    txt[2] = "玩家3";
    txt[3] = "玩家4";
    txt[4] = "玩家5";
    txt[5] = "玩家6";

    l_player[0] = new QLabel("玩家1");
    l_player[1] = new QLabel("玩家2");
    l_player[2] = new QLabel("玩家3");
    l_player[3] = new QLabel("玩家4");
    l_player[4] = new QLabel("玩家5");
    l_player[5] = new QLabel("玩家6");

    for(int i=0; i<6; i++){
        l_player[i]->setFixedSize(300,100);
    }

    w_abandon = new QLabel("弃牌区：");
    w_public = new QLabel("公示区：");

    w_abandon->setFixedSize(100 ,100);
    w_public->setFixedSize(100 ,100);

    VLayou1 = new QVBoxLayout;
    VLayou2 = new QVBoxLayout;
    VLayou3 = new QVBoxLayout;
    VLayou4 = new QVBoxLayout;
    VLayou5 = new QVBoxLayout;
    HLayou = new QHBoxLayout;

    VLayou1->addWidget(l_player[0]);
    VLayou1->addWidget(l_player[1]);
    VLayou1->addWidget(l_player[2]);
    VLayou1->addWidget(w_abandon);

    VLayou2->addWidget(l_player[3]);
    VLayou2->addWidget(l_player[4]);
    VLayou2->addWidget(l_player[5]);
    VLayou2->addWidget(w_public);

    VLayou3->addWidget(pushbtn);
    VLayou3->addWidget(btn_change);
    VLayou3->addWidget(btn_check);
    VLayou3->addWidget(btn_reject);
    VLayou3->addWidget(btn_pub);
    VLayou3->addWidget(btn_show);

    VLayou4->addWidget(pushbtn_chip);
    VLayou4->addWidget(spin_raise);
    VLayou4->addWidget(btn_raise);
    VLayou4->addWidget(btn_call);
    VLayou4->addWidget(btn_fold);
    VLayou4->addWidget(btn_all);


    HLayou->addLayout(VLayou1);
    HLayou->addLayout(VLayou2);
    HLayou->addLayout(VLayou3);
    HLayou->addLayout(VLayou4);

    VLayou5->addLayout(HLayou);
    VLayou5->addWidget(record);

    setLayout(VLayou5);
}

Widget::~Widget()
{
    delete ui;
    delete w_deck;
}

void Widget::createShuffledarr(int arr[15])
{
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    std::default_random_engine engine(seed);
    std::shuffle(arr, arr + 15, engine);

}

//开始游戏
void Widget::game_start()
{
    createShuffledarr(Shuffled);//打乱数组

    //将这个数组打包给客户端
    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，3表示整形数组类型+字符串
    out << 3 ;

    // 整形数组
    for (int i = 0; i < 15; ++i) {
        out << Shuffled[i];
    }

    out << QString("kaishi");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }

    w_deck->createShuffledCardDeck(Shuffled);
    w_deck->begin();

    record->clear();
    record->appendPlainText("新的一轮游戏开始");
    record->appendPlainText("你的底牌是" + QString::number(w_deck->m_player[w_deck->my_plyr-1]->get_rank()));
    record->appendPlainText("现在是玩家" + QString::number(w_deck->m_num%6 + 1) + "的回合");

//    for (int i = 0; i < 6; i++) {
//        qDebug() << w_deck->m_player[i]->get_rank();
//    }

    for(int i=0;i<6;i++){
        if (w_deck->m_player[i]->get_rank()%2==0){
            l_player[i]->setStyleSheet("background-color: blue;");
        }
        else l_player[i]->setStyleSheet("background-color: green;");
    }

    //更新每个人的剩余筹码
    QString text[6];
    for(int i =0 ; i< 6 ; i++){
        txt[i] = "玩家" + QString::number(i + 1) + "\n筹码：" + QString::number(w_deck->m_player[i]->get_chip()) + "\n当前下注:";
        text[i] = txt[i] + "10";//底注
        l_player[i]->setText(text[i]);
    }

    w_abandon->setText("弃牌区：");
    w_public->setText("公示区：");

    if_me();

    pushbtn->setEnabled(false);
}

void Widget::start_chip()
{
    if (w_deck->my_plyr == w_deck->m_num%6+1)
    {
        btn_raise->setEnabled(true);
        btn_call->setEnabled(true);
        btn_fold->setEnabled(true);
        btn_all->setEnabled(true);
    }

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("startchip");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }
}

//开牌
void Widget::show_card()
{
    //亮牌
    QString text[6];
    for (int i = 0; i < 6; i++) {
        text[i] = "玩家" + QString::number(i+1) + "底牌为 " + QString::number(w_deck->m_player[i]->get_rank()) + "\n当前下注:" + QString::number(w_deck->m_player[i]->get_c_chip());
        l_player[i]->setText(text[i]);
    }

    //实现游戏结果的判定
    up_score(w_deck->card_pub , w_deck->m_player);

//    int winner = 0;
    int maxscore = 0;

    for(int i= 0; i<6 ; i++){
        if(w_deck->m_player[i]->if_stay){
            if(w_deck->m_player[i]->score > maxscore){
                maxscore = w_deck->m_player[i]->score;
//                winner = i ;
            }
        }
    }

    int chip_sum = 0;
    int chip_pre = 0;
    int win_num = 0;
    for(int i=0; i<6; i++){
        chip_sum += w_deck->m_player[i]->get_c_chip();
        if(w_deck->m_player[i]->score == maxscore){
            win_num++;
        }
    }

    chip_pre = chip_sum / win_num;

    QString txt;

    for(int i=0; i<6; i++){
        if(w_deck->m_player[i]->score == maxscore){
            w_deck->m_player[i]->change_chip(w_deck->m_player[i]->get_chip() + chip_pre - w_deck->m_player[i]->get_c_chip());

            txt.append(QString::number(i + 1) + " ");
        }
    }

    record->appendPlainText("玩家" + txt + "赢得筹码！本轮总下注为" + QString::number(chip_sum));
//    for(int i=0; i<6; i++){
//        if(i!=winner){
//            chip_sum += w_deck->m_player[i]->get_c_chip();
//            w_deck->m_player[i]->change_chip(w_deck->m_player[i]->get_chip() -  w_deck->m_player[i]->get_c_chip());
//        }
//    }

//    w_deck->m_player[winner]->change_chip(w_deck->m_player[winner]->get_chip() + chip_sum);

//    record->appendPlainText("玩家" + QString::number(winner + 1) + "赢得所有筹码！本轮总下注为" + QString::number(chip_sum + w_deck->m_player[winner]->get_c_chip()));

    pushbtn->setEnabled(true);
    btn_show->setEnabled(false);

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型
    out << 6 ;

    out << maxscore;

    for (int i = 0; i < 6; ++i) {
        out << w_deck->m_player[i]->score;
    }

    out << maxscore << QString("kaipai");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }
}


void Widget::if_change()
{
//    QString text = "玩家" + QString::number(w_deck->m_num%6 + 1) + "可换的牌: " + QString::number(w_deck->card_deck.at(w_deck->m_num)->get_rank());
//    QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip()) + "\n可换的牌:" + QString::number(w_deck->card_deck.at(w_deck->m_num)->get_rank());
//    l_player[w_deck->m_num%6]->setText(text);
    record->appendPlainText("你可以选择换的牌是 " + QString::number(w_deck->card_deck.at(w_deck->m_num)->get_rank()));
    btn_check->setEnabled(true);
    btn_reject->setEnabled(true);
    btn_change->setEnabled(false);
    btn_pub->setEnabled(false);

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("change");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }
}

void Widget::c_change()
{
    QString text_1 = w_abandon->text() + " " + QString::number(w_deck->m_player[w_deck->m_num%6]->get_rank()) + " ,";
    w_abandon->setText(text_1);

    w_deck->m_player[w_deck->m_num%6]->set_hand(w_deck->card_deck.at(w_deck->m_num));
    w_deck->m_player[w_deck->m_num%6]->score = w_deck->card_deck.at(w_deck->m_num)->get_rank();

    if(w_deck->m_player[w_deck->m_num%6]->get_rank()%2==0){
                    l_player[w_deck->m_num%6]->setStyleSheet("background-color: blue;");
                }
                else l_player[w_deck->m_num%6]->setStyleSheet("background-color: green;");

    record->appendPlainText("现在你的底牌是" + QString::number(w_deck->m_player[w_deck->my_plyr-1]->get_rank()));


//    QString text = "玩家" + QString::number(w_deck->m_num%6 + 1);
//    QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip());
//    l_player[w_deck->m_num%6]->setText(text);

    btn_check->setEnabled(false);
    btn_reject->setEnabled(false);
//    btn_change->setEnabled(true);
//    btn_pub->setEnabled(true);

    w_deck->next_plyr();

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("cchange");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }
}

void Widget::r_change()
{
    //将这张牌放到弃牌区
//    QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip());
//    l_player[w_deck->m_num%6]->setText(text);

    QString text_1 = w_abandon->text() + " " + QString::number(w_deck->card_deck.at(w_deck->m_num)->get_rank()) + " ,";
    w_abandon->setText(text_1);

    record->appendPlainText("现在你的底牌是" + QString::number(w_deck->m_player[w_deck->my_plyr-1]->get_rank()));

    btn_check->setEnabled(false);
    btn_reject->setEnabled(false);
//    btn_change->setEnabled(true);
//    btn_pub->setEnabled(true);

    w_deck->next_plyr();

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("rchange");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }
}

void Widget::if_pub()
{
    QString text = w_public->text() + " " + QString::number(w_deck->card_deck.at(w_deck->m_num)->get_rank()) + " ,";
    w_public->setText(text);

    w_deck->card_pub.insert(w_deck->card_pub.begin() , w_deck->card_deck.at(w_deck->m_num)->get_rank());

    w_deck->next_plyr();

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("pub");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }

    btn_change->setEnabled(false);
    btn_pub->setEnabled(false);

}

void Widget::w_raise()
{

    if(w_deck->m_player[w_deck->m_num%6]->if_stay)
    {
        w_deck->m_chip = spin_raise->value() + w_deck->m_chip;
        w_deck->m_player[w_deck->m_num%6]->change_c_chip(w_deck->m_chip);

        QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip());
        l_player[w_deck->m_num%6]->setText(text);

        w_deck->m_left = 5 - w_deck->m_all - w_deck->m_fold;
    }

    w_deck->next_plyr();

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，5表示整型变量+字符串
    out << 5 ;

    out << spin_raise->value() << QString("raise");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }

    btn_raise->setEnabled(false);
    btn_call->setEnabled(false);
    btn_fold->setEnabled(false);
    btn_all->setEnabled(false);

}

void Widget::w_fold()
{
    w_deck->m_player[w_deck->m_num%6]->if_stay = false;

    QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip()) + "\n已弃牌";
    l_player[w_deck->m_num%6]->setText(text);

    w_deck->m_left--;
    w_deck->m_fold++;

    if_end();

    w_deck->next_plyr();

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("fold");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }

    btn_raise->setEnabled(false);
    btn_call->setEnabled(false);
    btn_fold->setEnabled(false);
    btn_all->setEnabled(false);
}

void Widget::w_call()
{
    if(w_deck->m_player[w_deck->m_num%6]->if_stay)
    {
         w_deck->m_player[w_deck->m_num%6]->change_c_chip(w_deck->m_chip);

         QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip());
         l_player[w_deck->m_num%6]->setText(text);

         w_deck->m_left--;

         if_end();
    }

    w_deck->next_plyr();

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("call");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }

    btn_raise->setEnabled(false);
    btn_call->setEnabled(false);
    btn_fold->setEnabled(false);
    btn_all->setEnabled(false);
}

void Widget::w_all()
{
    if(w_deck->m_player[w_deck->m_num%6]->if_stay)
    {
         w_deck->m_player[w_deck->m_num%6]->change_c_chip(w_deck->m_player[w_deck->m_num%6]->get_chip());
         w_deck->m_player[w_deck->m_num%6]->if_all = true ;

         QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip()) + "\n已all-in";
         l_player[w_deck->m_num%6]->setText(text);

         w_deck->m_left--;
         w_deck->m_all++;

         if_end();
    }

    w_deck->next_plyr();

    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);

    // 数据类型，2表示字符串
    out << 2 ;

    out << QString("all");

    // 将数据发送给所有客户端
    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }

    btn_raise->setEnabled(false);
    btn_call->setEnabled(false);
    btn_fold->setEnabled(false);
    btn_all->setEnabled(false);
}

void Widget::start_listen()
{
    unsigned short port = w_port->toPlainText().toUShort();
    m_s->listen(QHostAddress::Any , port);
    btn_tcps->setEnabled(false);
    btn_start->setEnabled(true);
    w_record->appendPlainText("服务器已启动,等待玩家连接......");
    w_record->appendPlainText("你是玩家1");
}

void Widget::if_me()
{
    if((w_deck->my_plyr == w_deck->m_num%6+1)&&(w_deck->m_num < 12 + w_deck->m_dealer)){
        btn_change->setEnabled(true);
        btn_pub->setEnabled(true);
    }
}

void Widget::if_mechip()
{
    if((w_deck->my_plyr == w_deck->m_num%6+1)&&(w_deck->m_left != 0)){
        if (w_deck->m_player[w_deck->m_num%6]->if_stay&&!(w_deck->m_player[w_deck->m_num%6]->if_all))
        {
            btn_raise->setEnabled(true);
            btn_call->setEnabled(true);
            btn_fold->setEnabled(true);
            btn_all->setEnabled(true);
        }
        else {
            w_deck->next_plyr();
            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("next");
        }
    }
}

void Widget::if_end()
{
    if(w_deck->m_left == 0){
        btn_show->setEnabled(true);
        btn_raise->setEnabled(false);
        btn_call->setEnabled(false);
        btn_fold->setEnabled(false);
        btn_all->setEnabled(false);
    }
}

//执行完这个函数后，玩家的score会更新，最后只需从在场玩家中找到最大score玩家即可
void Widget::up_score(std::vector<int> &cardPool, Player * plyrs[6])
{
    std::vector<int> validCards(cardPool.begin(), cardPool.begin() + std::min(3, static_cast<int>(cardPool.size())));

        while (!validCards.empty()) {
            int maxCard = *std::max_element(validCards.begin(), validCards.end());
            std::vector<Player*> players;

            for (int i = 0;i< 6; i++) {
                if ((plyrs[i]->if_stay)&&(!(plyrs[i]->if_match))) {
                    players.push_back(plyrs[i]);
                }
            }

            std::sort(players.begin(), players.end(),
                []( Player* a,  Player* b) { return a->get_rank() < b->get_rank(); });

            players.front()->score += maxCard;
            validCards.erase(std::remove(validCards.begin(), validCards.end(), maxCard), validCards.end());
            players.front()->if_match = true;
            qDebug() <<"底牌最小玩家已配对，其if_match成员当前值为" << players.front()->if_match;
        }
        for(int i=0;i<6;i++)
        {qDebug() << plyrs[i]->score ;}
}

void Widget::analyze()
{
    QByteArray byteArray = m_tcp.at(w_deck->m_num%6-1)->readAll();
    QDataStream in(&byteArray, QIODevice::ReadOnly);

    int dataType;
    in >> dataType;

    if (dataType == 2) {
        // 读取字符串
        QString signalName;
        in >> signalName;

        // 处理字符串数据
        //当前客户端玩家选择换牌
        if (signalName == "change") {
            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "选择换牌");

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("change");

            for (QTcpSocket *socket : m_tcp) {
                if(socket != m_tcp.at(w_deck->m_num%6-1))
                {socket->write(data);}
            }
        }

        // 当前客户端玩家确认换牌
        if (signalName == "cchange") {
            QString text_1 = w_abandon->text() + " " + QString::number(w_deck->m_player[w_deck->m_num%6]->get_rank()) + " ,";
            w_abandon->setText(text_1);

            w_deck->m_player[w_deck->m_num%6]->set_hand(w_deck->card_deck.at(w_deck->m_num));
            w_deck->m_player[w_deck->m_num%6]->score = w_deck->card_deck.at(w_deck->m_num)->get_rank();

            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "确认换牌");

            w_deck->next_plyr();

            if_me();

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("cchange");

            for (QTcpSocket *socket : m_tcp) {
                int tcp = ((w_deck->m_num%6-2)>-1?w_deck->m_num%6-2:4);
                if(socket != m_tcp.at(tcp))
                {socket->write(data);}
            }
        }

        if (signalName == "rchange") {
            QString text_1 = w_abandon->text() + " " + QString::number(w_deck->card_deck.at(w_deck->m_num)->get_rank()) + " ,";
            w_abandon->setText(text_1);

            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "放弃换牌");

            w_deck->next_plyr();

            if_me();

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("rchange");

            for (QTcpSocket *socket : m_tcp) {
                int tcp = ((w_deck->m_num%6-2)>-1?w_deck->m_num%6-2:4);
                if(socket != m_tcp.at(tcp))
                {socket->write(data);}
            }
        }

        if (signalName == "pub"){
            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "选择公示");

            QString text = w_public->text() + " " + QString::number(w_deck->card_deck.at(w_deck->m_num)->get_rank()) + " ,";
            w_public->setText(text);

            w_deck->card_pub.insert(w_deck->card_pub.begin() , w_deck->card_deck.at(w_deck->m_num)->get_rank());

            w_deck->next_plyr();

            if_me();

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("pub");

            for (QTcpSocket *socket : m_tcp) {
                int tcp = ((w_deck->m_num%6-2)>-1?w_deck->m_num%6-2:4);
                if(socket != m_tcp.at(tcp))
                {socket->write(data);}
            }

        }

        if (signalName == "fold"){
            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "选择弃牌");

            w_deck->m_player[w_deck->m_num%6]->if_stay = false;

            QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip()) + "\n已弃牌";
            l_player[w_deck->m_num%6]->setText(text);

            w_deck->m_left--;
            w_deck->m_fold++;

            if_end();

            w_deck->next_plyr();

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("fold");

            for (QTcpSocket *socket : m_tcp) {
                int tcp = ((w_deck->m_num%6-2)>-1?w_deck->m_num%6-2:4);
                if(socket != m_tcp.at(tcp))
                {socket->write(data);}
            }

            emit chips();
        }

        if (signalName == "call"){
//            qDebug() << "服务器收到客户端的call信号" ;
            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "选择跟注");

            w_deck->m_player[w_deck->m_num%6]->change_c_chip(w_deck->m_chip);

            QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip());
            l_player[w_deck->m_num%6]->setText(text);

            w_deck->m_left--;

            if_end();

            w_deck->next_plyr();

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("call");

            for (QTcpSocket *socket : m_tcp) {
                int tcp = ((w_deck->m_num%6-2)>-1?w_deck->m_num%6-2:4);
                if(socket != m_tcp.at(tcp))
                {socket->write(data);}
            }

            emit chips();
        }

        if (signalName == "all"){
            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "选择all-in");

            w_deck->m_player[w_deck->m_num%6]->change_c_chip(w_deck->m_player[w_deck->m_num%6]->get_chip());
            w_deck->m_player[w_deck->m_num%6]->if_all = true ;

            QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip()) + "\n已all-in";
            l_player[w_deck->m_num%6]->setText(text);

            w_deck->m_left--;
            w_deck->m_all++;

            if_end();

            w_deck->next_plyr();

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型，2表示字符串
            out << 2 ;

            out << QString("all");

            for (QTcpSocket *socket : m_tcp) {
                int tcp = ((w_deck->m_num%6-2)>-1?w_deck->m_num%6-2:4);
                if(socket != m_tcp.at(tcp))
                {socket->write(data);}
            }

            emit chips();
        }

        if (signalName == "next"){
            w_deck->next_plyr();

            emit chips();
        }
}

    else if (dataType == 1) {
        // 读取整型数组
        for (int i = 0; i < 15; i++) {
            in >> Shuffled[i];
        }
        // 处理整型数组数据
}
    else if (dataType == 3){
        for (int i = 0; i < 15; i++) {
            in >> Shuffled[i];
        }

        QString signalName;
        in >> signalName;
    }

    else if (dataType == 5){
        QString signalName;
        int chip;

        in >> chip >> signalName;
        if(signalName == "raise"){
            w_deck->m_chip = chip + w_deck->m_chip;
            w_deck->m_player[w_deck->m_num%6]->change_c_chip(w_deck->m_chip);

            QString text = txt[w_deck->m_num%6] + QString::number(w_deck->m_player[w_deck->m_num%6]->get_c_chip());
            l_player[w_deck->m_num%6]->setText(text);

            record->appendPlainText("玩家" + QString::number(w_deck->m_num%6+1) + "选择加注到" + QString::number(w_deck->m_chip));

            w_deck->m_left = 5 - w_deck->m_all - w_deck->m_fold;

            w_deck->next_plyr();

            QByteArray data;
            QDataStream out(&data, QIODevice::WriteOnly);

            // 数据类型
            out << 5 ;

            out << chip << QString("raise");

            for (QTcpSocket *socket : m_tcp) {
                int tcp = ((w_deck->m_num%6-2)>-1?w_deck->m_num%6-2:4);
                if(socket != m_tcp.at(tcp))
                {socket->write(data);}
            }

            emit chips();
        }

    }

}

void Widget::start_game()
{
    newidget->close();
    this->show();

    for(int i =0;i< m_tcp.size();i++)
    {connect(m_tcp.at(i),&QTcpSocket::readyRead,this,analyze);}

    record->appendPlainText("你是玩家1");


    QByteArray data;
    QDataStream out(&data, QIODevice::WriteOnly);
    // 将需要发送的数据打包成 QByteArray 的形式
    out << 2 << QString("gamestart");
    // 发送数据给所有连接到服务器的客户端

    for (QTcpSocket *socket : m_tcp) {
        socket->write(data);
    }
}


